package com.laijiale.mapper;

import com.laijiale.entities.Permission;
import com.laijiale.entities.Role;
import com.laijiale.entities.RolePermission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:51
 */
@Repository
public interface PermissionMapper {
    @Insert("insert into ums_permission (title,url,icon,pid,info,deleted,created,updated )" +
            "values(#{title},#{url},#{icon},#{pid},#{info},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Permission permission);


    @Select("select up.id, up.title,up.url,up.icon,up.pid,up.info ,up.deleted, up.created, up.updated \n" +
            "from ums_permission up" +
            " where up.id=#{param1} and up.deleted=0")
    Permission selectById(Long id);

    @Update("<script>" +
            "update ums_permission" +
            "<set>" +
            "<if test='title !=null'>title=#{title},</if> " +
            "<if test='url !=null'>url=#{url},</if> " +
            "<if test='icon !=null'>icon=#{icon},</if> " +
            "<if test='pid !=null'>pid=#{pid},</if> " +
            "<if test='info !=null'>info=#{info},</if> " +
            "<if test='deleted !=null'>deleted=#{deleted},</if> " +

            "<if test='created !=null'>created=#{created},</if> " +

            "<if test='updated !=null'>updated=#{updated},</if> " +
            "</set> " +
            "where id =#{id} and deleted =0" +
            "</script>")
    int updateById(Permission permission);

    @Update("update ums_permission set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);
    @Update("<script>" +
            "update ums_permission set deleted =1,updated = now() where " +
            "<foreach collection = 'list' item='e' open ='id in (' close= ')' separator = ','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);
    @Results(id = "resultMap",value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "parentPermission",
                    one = @One(select = "com.laijiale.mapper.PermissionMapper.selectById")),
    })
@Select("select  up.id, up.title,up.url,up.icon,up.pid,up.info, up.deleted, up.created, up.updated from ums_permission  up where up.deleted=0")
    List<Permission> list();

    @Select("select up.id, up.title from ums_permission up " +
            "where up.title = #{title} ")
     Permission selectByAllTitle(Permission permission);

    @Delete("delete from ums_permission where id =#{param1}")
    int deleteById(Long id);

    @Select("select up.id, up.title from ums_permission up " +
            "where up.title = #{param1} ")
    Permission selectByTitle(String title);

    @Select("select up.id, up.title, up.url, up.icon, up.pid, " +
            "up.deleted, up.created, up.updated " +
            "from ums_permission up where up.id in( " +
            "select urp.fk_permission_id from ums_role_permission urp " +
            "where fk_role_id in ( " +
            "select ue.fk_role_id from ums_emp_role ue where ue.fk_emp_id = #{param1}))")
    List<Permission> selectByEmpId(Long empId);

    @Select("select up.id, up.title, up.url, up.icon, up.pid, " +
            "up.deleted, up.created, up.updated " +
            "from ums_role_permission urp join ums_role ur on urp.fk_role_id=ur.id join ums_permission up on " +
            "urp.fk_permission_id = up.id where urp.fk_role_id=#{param1}  " +
            "and ur.deleted = 0 and urp.deleted=0")
    List<Permission> selectByRoleId(Long roleId);

    @Update("update ums_role_permission set deleted = 1, updated = now() where fk_role_id = #{param1}")
    int logicDeleteByRoleId(Long RoleId);

    @Insert("insert into ums_role_permission (fk_role_id,fk_permission_id,deleted,created,updated) " +
            "values(#{fkRoleId},#{fkPermissionId},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRolePermission(RolePermission rolePermission);

    @Select("select up.id, up.title, up.url, up.icon, up.pid, up.info, " +
            "up.deleted, up.created, up.updated from ums_permission up " +
            "where up.deleted = 0 and up.pid = 0")
    List<Permission> listParents();
    @Select("select  up.id, up.title,up.url,up.icon,up.pid ,up.info,up.deleted, " +
            "up.created,up.updated  from ums_permission up "+
            "where up.deleted=0 and up.title like concat('%',#{param1},'%') "
    )
    List<Permission> listLikeTitle(String title);


}
